﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;

namespace Project01_DBSecurity
{
    public partial class Form_Login : Form
    {
		private bool loggedin = false;

		private void Form_Login_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (loggedin == false)
				Application.Exit();
		}

        public Form_Login()
        {
            InitializeComponent();
			this.FormClosing += new FormClosingEventHandler(Form_Login_FormClosing);
        }

        private void btConnect_Click(object sender, EventArgs e)
        {
			orcl.conn = new OracleConnection();
			orcl.conn.ConnectionString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" +
				tbHostname.Text + ")(PORT=" +
				tbPort.Text + ")))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=" +
				tbSID.Text + ")));User Id=" +
				tbUsername.Text + ";Password=" +
				tbPassword.Text + ";";
			if (cbRole.SelectedIndex == 1)
				orcl.conn.ConnectionString += "DBA Privilege=SYSDBA;";

			try
			{
				orcl.conn.Open();
				if (orcl.conn.State.ToString() == "Open")
				{
					loggedin = true;
					this.Close();
				}
				else
				{
					MessageBox.Show("kết nối thất bại", "info", MessageBoxButtons.OK, MessageBoxIcon.Error);
					orcl.conn.Dispose();
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
        }

        private void btQuit_Click(object sender, EventArgs e)
        {
			this.Close();
        }

        private void Form_Login_Load(object sender, EventArgs e)
        {
			cbRole.Items.Add("Normal");
			cbRole.Items.Add("SYSDBA");
			cbRole.SelectedIndex = 0;
        }

    }
}
